﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;
using BLL.PayRecordsTransaction;
using BLL;
using BLL.BaseDictionaryService;

namespace FamilyManager
{
    public partial class PayRecordQueryForm : Form
    {
        private PayRecordQueryForm()
        {
            InitializeComponent();
        }

        private static PayRecordQueryForm instance;

        public static PayRecordQueryForm CreateInstance()
        {
            if (instance == null)
            {
                instance = new PayRecordQueryForm();
            }
            return instance;
        }

        private void BindcbUsers()
        {
            QueryUsersTransaction command = new QueryUsersTransaction();
            command.Execute();
            T_UserObject tmpUser = new T_UserObject();
            tmpUser.Db_NickName = "";
            tmpUser.Db_OID = 0;
            command.Users.Insert(0, tmpUser);
            cbPayObject.DataSource = command.Users;
            cbPayObject.DisplayMember = "Db_NickName";
            cbPayObject.ValueMember = "Db_OID";
        }

        private void BindcbPayNecessity()
        {
            QueryPayNecessityTransaction command = new QueryPayNecessityTransaction();
            command.Execute();
            D_PayNecessityObject tmpD_PayNecessityObject = new D_PayNecessityObject();
            tmpD_PayNecessityObject.Db_PayNecessity = "";
            tmpD_PayNecessityObject.Db_OID = 0;
            command.PayNecessitys.Insert(0, tmpD_PayNecessityObject);
            cbPayNecessity.DataSource = command.PayNecessitys;
            cbPayNecessity.DisplayMember = "Db_PayNecessity";
            cbPayNecessity.ValueMember = "Db_OID";
        }

        private void BindcbPayCategory()
        {
            QueryPayCategoryTransaction command = new QueryPayCategoryTransaction();
            command.Execute();
            D_PayCategoryObject tmpD_PayCategoryObject = new D_PayCategoryObject();
            tmpD_PayCategoryObject.Db_CategoryName = "";
            tmpD_PayCategoryObject.Db_OID = 0;
            command.PayCategorys.Insert(0, tmpD_PayCategoryObject);
            this.cbPayCategory.DataSource = command.PayCategorys;
            this.cbPayCategory.ValueMember = "Db_OID";
            this.cbPayCategory.DisplayMember = "Db_CategoryName";
        }


        private void BindgvPayRecords()
        {
            gvResults.Columns.Clear();
            gvResults.AutoGenerateColumns = false;

            NomalQueryPayRecordsTransaction objNomalQueryPayRecordsTransaction = new NomalQueryPayRecordsTransaction(CreateCondition());
            gvResults.DataSource = objNomalQueryPayRecordsTransaction.Query();

            DataGridViewTextBoxColumn dcOID = new DataGridViewTextBoxColumn();
            dcOID.HeaderText = "记录序号";
            dcOID.DataPropertyName = "Db_OID";
            gvResults.Columns.Add(dcOID);
            dcOID.Visible = false;

            DataGridViewTextBoxColumn dcPayDate = new DataGridViewTextBoxColumn();
            dcPayDate.HeaderText = "消费日期";
            dcPayDate.DataPropertyName = "Db_PayDate";
            gvResults.Columns.Add(dcPayDate);

            DataGridViewTextBoxColumn dcPayItem = new DataGridViewTextBoxColumn();
            dcPayItem.HeaderText = "消费项目";
            dcPayItem.DataPropertyName = "Db_PayItem";
            gvResults.Columns.Add(dcPayItem);

            DataGridViewTextBoxColumn dcPayObject = new DataGridViewTextBoxColumn();
            dcPayObject.HeaderText = "消费对象";
            dcPayObject.DataPropertyName = "UserNickName";
            gvResults.Columns.Add(dcPayObject);

            DataGridViewTextBoxColumn dcPayCategoryOID = new DataGridViewTextBoxColumn();
            dcPayCategoryOID.HeaderText = "消费类别";
            dcPayCategoryOID.DataPropertyName = "PayCategory";
            gvResults.Columns.Add(dcPayCategoryOID);

            DataGridViewTextBoxColumn dcPaySum = new DataGridViewTextBoxColumn();
            dcPaySum.HeaderText = "消费金额";
            dcPaySum.DataPropertyName = "Db_PaySum";
            gvResults.Columns.Add(dcPaySum);

            DataGridViewTextBoxColumn dcPayNecessity = new DataGridViewTextBoxColumn();
            dcPayNecessity.HeaderText = "必要性";
            dcPayNecessity.DataPropertyName = "PayNecessity";
            gvResults.Columns.Add(dcPayNecessity);

            DataGridViewTextBoxColumn dcPayMemo = new DataGridViewTextBoxColumn();
            dcPayMemo.HeaderText = "消费备注";
            dcPayMemo.DataPropertyName = "Db_PayMemo";
            gvResults.Columns.Add(dcPayMemo);

        }

        /// <summary>
        /// 普通查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNomalQuery_Click(object sender, EventArgs e)
        {
            BindgvPayRecords();
        }

        private T_PayRecordsObject CreateCondition()
        {
            T_PayRecordsObject condition = new T_PayRecordsObject();
            condition.BeginDate = dtpBeginDate.Value.Date;
            condition.EndDate = dtpEndDate.Value.Date;
            condition.Db_PayItem = txtPayItem.Text;
            if (cbPayCategory.Text != string.Empty)
            {
                condition.Db_PayCategoryOID = int.Parse(cbPayCategory.SelectedValue.ToString());
            }
            else
            {
                condition.Db_PayCategoryOID = 0;
            }
            if (cbPayNecessity.Text != string.Empty)
            {
                condition.Db_PayNecessity = int.Parse(cbPayNecessity.SelectedValue.ToString());
            }
            else
            {
                condition.Db_PayNecessity = 0;
            }
            if (cbPayObject.Text != string.Empty)
            {
                condition.Db_PayObject = int.Parse(cbPayObject.SelectedValue.ToString());
            }
            else
            {
                condition.Db_PayObject = 0;
            }
            return condition;
        }

        /// <summary>
        /// 高级查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdvancedQuery_Click(object sender, EventArgs e)
        {
            
        }

        private void btnStatistic_Click(object sender, EventArgs e)
        {
            if (gvResults.Rows.Count == 0) return;

            IList<int> recordsOIDs = new List<int>();
            foreach (DataGridViewRow row in gvResults.Rows)
            {
                recordsOIDs.Add(int.Parse(row.Cells[0].Value.ToString()));
            }
            PayRecordStatisticsForm objPayRecordStatisticsForm = new PayRecordStatisticsForm(recordsOIDs);
            objPayRecordStatisticsForm.ShowDialog();
        }

        private void PayRecordQueryForm_Load(object sender, EventArgs e)
        {
            BindcbUsers();
            BindcbPayCategory();
            BindcbPayNecessity();
            BindgvPayRecords();
        }

        private void rbtnrbtnPreviousMonth_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnrbtnPreviousMonth.Checked)
            {
                DateTime dtPreviousMonth = DateTime.Now.AddMonths(-1);
                dtpBeginDate.Value = new DateTime(dtPreviousMonth.Year, dtPreviousMonth.Month, 1);
                dtpEndDate.Value = new DateTime(dtPreviousMonth.Year, dtPreviousMonth.Month, ToolBox.GetEndDay(dtPreviousMonth.Year,dtPreviousMonth.Month));

            }
        }

        private void rbtnrbtnPreviousYear_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnrbtnPreviousYear.Checked)
            {
                DateTime dtPreviousMonth = DateTime.Now.AddYears(-1);
                dtpBeginDate.Value = new DateTime(dtPreviousMonth.Year, 1, 1);
                dtpEndDate.Value = new DateTime(dtPreviousMonth.Year, 12, 31);
            }
        }

        private void rbtnToday_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnToday.Checked)
            {
                dtpBeginDate.Value = DateTime.Now;
                dtpEndDate.Value = DateTime.Now; 
            }
        }
    }
}
